package org.nidy.programexercise.IOExercise;

import java.io.*;

public class CharacterStream {
    public static void main(String[] args) throws IOException {
        System.out.println("Character Stream");
        System.out.println("==================字符输入流==========================");
        //1、创建字符输入流
        FileReader fr=new FileReader("src\\main\\java\\org\\nidy\\programexercise\\IOExercise\\c.txt");
        //2、读取文件内容
        char[] ch=new char[1024];
        int len=0;
        while ((len=fr.read(ch))!=-1){
            System.out.println(new String(ch,0,len));
        }
        //3、关闭流
        fr.close();
        System.out.println("==================字符输出流==========================");
        FileWriter fw=new FileWriter("src\\main\\java\\org\\nidy\\programexercise\\IOExercise\\d.txt");
        fw.write("hello world");
        fw.close();
        System.out.println("==================字符缓冲输入流==========================");
        BufferedReader br=new BufferedReader(new FileReader("src\\main\\java\\org\\nidy\\programexercise\\IOExercise\\c.txt"));
        //字符缓冲流readline()方法，一次读取一行【String line = br.readLine();】
        String line = null;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
        System.out.println("==================字符缓冲输出流==========================");
        BufferedWriter bw=new BufferedWriter(new FileWriter("src\\main\\java\\org\\nidy\\programexercise\\IOExercise\\d.txt"));
        bw.write("hello world");
        bw.newLine();
        bw.close();
    }
}
